﻿<template>
    <div class="esMerchantInterfaceTemplate-container">
        <el-card shadow="hover">
            <el-form :model="state.queryParams" ref="queryForm" :inline="true">
                <el-form-item :label-width='70' label="商户名称">
                    <el-select :clearable='true' class='ml10' v-model="state.queryParams.merchantId"
                               placeholder="请选择" filterable>
                        <el-option v-for='item in state.merchantDict' :key='item.value' :label='item.label'
                                   :value='item.value'/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button-group>
                        <el-button type="primary" icon="ele-Search" @click="handleQuery" :loading="state.loading"
                                   v-auth="'esMerchantInterfaceTemplate:query'"> 查询
                        </el-button>
                        <el-button icon="ele-Refresh" @click="resetQuery" v-auth="'esMerchantInterfaceTemplate:query'">
                            重置
                        </el-button>
                    </el-button-group>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card shadow="hover" style="margin-top: 8px">
            <div style="margin: -8px 0 10px 0;">
                <el-button type="success" plain icon="ele-Plus" @click="openAddEsMerchantInterfaceTemplate"
                           v-auth="'esMerchantInterfaceTemplate:add'"> 新增
                </el-button>
            </div>
            <el-table
                :data="state.data"
                style="width: 100%"
                v-loading="state.loading"
                tooltip-effect="light"
                row-key="id"
                border stripe highlight-current-row>
                <el-table-column header-align="center" type="index" label="序号" width="55" align="center"/>
                <el-table-column header-align="center" prop="merchantName" label="商户接口模版" width="280"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="chargingMode" label="计费模式" min-width="120"
                                 align="center" show-overflow-tooltip>
                    <template #default="scope">
                        <div v-if="scope.row.chargingMode === 0">未知模式</div>
                        <div v-else-if="scope.row.chargingMode === 1">链接模式</div>
                        <div v-else-if="scope.row.chargingMode === 2">一次接口模式</div>
                        <div v-else-if="scope.row.chargingMode === 3">两次接口模式</div>
                        <div v-else-if="scope.row.chargingMode === 4">图形接口模式</div>
                        <div v-else-if="scope.row.chargingMode === 5">无异步回传接口模式</div>
                        <div v-else-if="scope.row.chargingMode === 6">外链模式</div>
                        <div v-else>未知</div>
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="httpMethod" label="请求方式" min-width="100" align="center"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="mimeType" label="MimeType" min-width="100" align="center"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="orderResult" label="订单结果依据" min-width="150"
                                 align="center" show-overflow-tooltip>
                    <template #default="scope">
                        <div v-if="scope.row.orderResult === -1">特殊通知为准</div>
                        <div v-else-if="scope.row.orderResult === 0">通知为准</div>
                        <div v-else-if="scope.row.orderResult === 1">验证码为准</div>
                        <div v-else-if="scope.row.orderResult === 2">查询订单状态为准</div>
                        <div v-else>未知</div>
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="reflection" label="服务程序" min-width="200" align="center"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="chargingLink" label="计费链接" min-width="100"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="verifyLink" label="验证链接" min-width="100"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="imageCodeLink" label="图验链接" min-width="100"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="checkOrTokenLink" label="Token或策略链接" min-width="160"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="orderLink" label="订单反馈链接" min-width="150"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="orderLinkReflection" label="订单反馈服务程序"
                                 min-width="150" align="center" show-overflow-tooltip/>
                <el-table-column header-align="center" prop="queryOrderLink" label="查询订单链接" min-width="150"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="queryOrderDelay" label="查询订单延时(sec)" min-width="150"
                                 align="right" show-overflow-tooltip/>
                <el-table-column header-align="center" prop="remark" label="备注" min-width="100"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="createTime" label="创建时间" width="160" align="center"
                                 show-overflow-tooltip/>
                <el-table-column label="操作" width="90" align="center" fixed="right" show-overflow-tooltip>
                    <template #default="scope">
                        <el-button size="small" text type="primary"
                                   @click="openEditEsMerchantInterfaceTemplate(scope.row)"
                                   v-auth="'esMerchantInterfaceTemplate:update'"> 编辑
                        </el-button>
                        <el-button size="small" text type="danger" @click="delEsMerchantInterfaceTemplate(scope.row)"
                                   v-auth="'esMerchantInterfaceTemplate:delete'"> 删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                           :page-sizes="[20, 50, 100]" v-model:current-page="state.queryParams.pageNum" background
                           v-model:page-size="state.queryParams.pageSize"
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="state.total"/>
            <EditEsMerchantInterfaceTemplate ref="editEsMerchantInterfaceTemplateRef" :title="state.editTitle"/>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="esMerchantInterfaceTemplate">

import {onMounted, onUnmounted, reactive, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import mittBus from '/@/utils/mitt';
import EditEsMerchantInterfaceTemplate
    from '/@/views/exocytosis/operate/merchantInterfaceTemplate/component/editEsMerchantInterfaceTemplate.vue';

import {EsMerchantInterfaceTemplate, sysBaseDict} from '/@/api/models';
import SysEsMerchantInterfaceTemplateApi from '/@/api/exocytosis/merchantInterfaceTemplate';
import SysEsMerchantApi from '/@/api/exocytosis/merchant';

// 定义变量内容
const editEsMerchantInterfaceTemplateRef = ref<InstanceType<typeof EditEsMerchantInterfaceTemplate>>();
const state = reactive({
    loading: false,
    data: [] as Array<EsMerchantInterfaceTemplate>,
    total: 0 as any,
    queryParams: {
        name: undefined,
        merchantId: undefined,
        pageNum: 1,
        pageSize: 20,
    },
    editTitle: '',
    merchantDict: [] as sysBaseDict[],
});

onMounted(async () => {
    await merchantDictQuery();
    mittBus.on('submitRefresh', () => {
        handleQuery();
    });
});

onUnmounted(() => {
    mittBus.off('submitRefresh');
});

// 查询操作
const handleQuery = async () => {
    state.loading = true;
    const res = await SysEsMerchantInterfaceTemplateApi().queryRecord(state.queryParams);
    state.data = res.data.result.items ?? [];
    state.total = res.data.result?.total;
    state.loading = false;
};

// 重置操作
const resetQuery = () => {
    // 1. 批量重置为undefined
    const keepKeys: string[] = ['pageNum', 'pageSize',]; // 声明需要保留的字段
    Object.keys(state.queryParams).forEach(key => {
        if (!keepKeys.includes(key))
            state.queryParams[key] = undefined;
    });
    // 2. 数据
    state.data = [];
};

// 打开新增页面
const openAddEsMerchantInterfaceTemplate = () => {
    state.editTitle = '添加';
    editEsMerchantInterfaceTemplateRef.value?.openDialog({
        chargingMode: 2,
        orderResult: 0,
        queryOrderDelay: 60,
        remark: '默认'
    });
};

// 打开编辑页面
const openEditEsMerchantInterfaceTemplate = (row: EsMerchantInterfaceTemplate) => {
    state.editTitle = '编辑';
    editEsMerchantInterfaceTemplateRef.value?.openDialog(row);
};

// 删除
const delEsMerchantInterfaceTemplate = (row: any) => {
    ElMessageBox.confirm(`确定删除：【${row.merchantName}】?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(async () => {
            let res = await SysEsMerchantInterfaceTemplateApi().deletePost({
                merchantId: row.merchantId,
                merchantTemplateIndex: row.merchantTemplateIndex
            });
            if (res.data.resCode != '0000')
                ElMessage.error(res.data.resMessage);
            else
                ElMessage.success("删除成功");
            await handleQuery();
        })
        .catch(() => {
        });
};

// 改变页面容量
const onHandleSizeChange = (val: number) => {
    state.queryParams.pageSize = val;
    handleQuery();
};

// 改变页码序号
const onHandleCurrentChange = (val: number) => {
    state.queryParams.pageNum = val;
    handleQuery();
};

// 商户列表
const merchantDictQuery = async () => {
    await SysEsMerchantApi().queryAll().then((res) => {
        if (res.data.resCode === '0000')
            state.merchantDict = res.data.result ?? [];
    });
};
</script>
